<?php
#基础函数
include_once('_path.php');

if (BENCHMARK) {
	require_once(_CONST_LOCAL_INCLUDE_PATH.'/Benchmark/Timer.php');
	$timer = new Benchmark_Timer();
	$timer->start();
}

#db
include_once(_CONST_LOCAL_INCLUDE_PATH."database.class.php");
$database = new Database($db_vars);

if ($db_slave_vars['hostname'] != '') {
	$slave = new Database($db_slave_vars);
} else {
	$slave =& $database;
}

#通用函数
include_once(_CONST_LOCAL_INCLUDE_PATH."common.php");

if (BENCHMARK) $timer->setMarker('init');

#用户信息
include_once(_CONST_LOCAL_INCLUDE_PATH."session.include.php");

if (BENCHMARK) $timer->setMarker('auth');

#载入model
load_model(array("vote","user"));

$uid = intval($_GET['id']);
#判断是否有这个用户
if(!$u = check_user($uid)){
  exit("No User!");
}
#翻页
$page = trim($_GET['page']) + 0;
if ($page < 1) $page = 1;
$count = get_user_vote_count($uid);
$total_page = ceil($count / 50);
if ($total_page < 1) $total_page = 1;
if ($page > $total_page) $page = $total_page;

#cache lite
include_once(_CONST_LOCAL_INCLUDE_PATH.'Cache/Lite.php');
include_once(_CONST_LOCAL_INCLUDE_PATH.'Cache/Lite/Output.php');

$cache = new Cache_Lite_Output();

#用于meta的keywords标签
$keywords = "";
#用于meta的Description标签
$description = "";
#用于mete的http-equiv标签
$referrer = "";
#用于meta的title标签
$title = "个人主页 - Votings";
#用于获取针对本页的css样式
$css = array("jquery.Jcrop");
#用于获取针对本页的js
$js = array("jquery.masonry.min", "jquery.infinitescroll.min", "ajaxupload", "jquery.Jcrop.min");
#用于获取针对head的其他标签
$links = array();
#用于控制那个主菜单为
$o = "homepage";

$ads = array();

$post_code =<<<HTML
HTML;

if (BENCHMARK) $timer->setMarker('proc');

//if ($user['loggedin'] < 1)
//	cache_header(900);

no_cache_header();

#header
include_once(_CONST_LOCAL_LAYOUT_PATH."_header.php");
?>
<script>
function vote_del(id)
{
	if (confirm("确定删除投票?")) {
		$.ajax({
			type: "GET",
			url: "/ajax/vote.php",
			data: "action=delete&vote_id="+id,
			success: function(msg){
			if(msg==1){
					alert("请先登录！");
					location.href='/login.php';
				}else{
					location.reload(true);
				}
			}
		});
	}
}

jQuery(function($){
    //hide page
    $("#page-nav-real").hide();
	//IMAGE STYLE
	var $container = $('#container');
	$container.imagesLoaded( function() {
		$container.masonry({
			itemSelector : '.pin',
			isAnimated: 'true'
		});
	});
	//page loading
	$container.infinitescroll({
		navSelector  : '#page-nav',    // selector for the paged navigation 
		nextSelector : '#page-nav a',  // selector for the NEXT link (to page 2)
		itemSelector : '.pin',     // selector for all items you'll retrieve
		loading: {
			finishedMsgShow:'#page-nav-real',
			finishedMsg: 'No more pages to load.',
			img:'media/bg/loadajax.gif',
			msgText:'loading...'
		},
		debug:true
    },
      // trigger Masonry as a callback
      function( newElements ) {
        // hide new items while they are loading
        var $newElems = $( newElements ).css({ opacity: 0 });
        // ensure that images load before adding to masonry layout
        $newElems.imagesLoaded(function(){
          // show elems now they're ready
          $newElems.animate({ opacity: 1 });
          $container.masonry( 'appended', $newElems, true ); 
        });
      }
    );
	
	//UPLOAD
	var button = $('#user-upload-avatar'), interval;
		
		new AjaxUpload(button, {
			action: 'ajax/upload-avatar-handler.php', 
			name: 'myfile',
			onSubmit : function(file, ext){
				// change button text, when user selects file			
				button.text('Uploading');
								
				// If you want to allow uploading only 1 file at time,
				// you can disable upload button
				this.disable();
				
				// Uploding -> Uploading. -> Uploading...
				interval = window.setInterval(function(){
					var text = button.text();
					if (text.length < 13){
						button.text(text + '.');					
					} else {
						button.text('Uploading');				
					}
				}, 200);
			},
			onComplete: function(file, response){
				button.text('Upload');
							
				window.clearInterval(interval);
							
				// enable upload button
				this.enable();
				if(response != 'error') {
					// add file to the list
					$("div.my-header-pic img").attr("src",response);
				}	
			}
		});
		
	   // Create variables (in this scope) to hold the API and image size
     /* var jcrop_api, boundx, boundy;
      
      $('#target').Jcrop({
        onChange: updatePreview,
        onSelect: updatePreview,
        aspectRatio: 1,
		setSelect: [ 60, 70, 180, 180 ]
      },function(){
        // Use the API to get the real image size
        var bounds = this.getBounds();
        boundx = bounds[0];
        boundy = bounds[1];
        // Store the API in the jcrop_api variable
        jcrop_api = this;
      });

      function updatePreview(c)
      {
        if (parseInt(c.w) > 0)
        {
          var ax = 180 / c.w;
          var ay = 180 / c.h;

          $('#preview-big').css({
            width: Math.round(ax * boundx) + 'px',
            height: Math.round(ay * boundy) + 'px',
            marginLeft: '-' + Math.round(ax * c.x) + 'px',
            marginTop: '-' + Math.round(ay * c.y) + 'px'
          });
		  
		  var bx = 50 / c.w;
          var by = 50 / c.h;

          $('#preview-middle').css({
            width: Math.round(bx * boundx) + 'px',
            height: Math.round(by * boundy) + 'px',
            marginLeft: '-' + Math.round(bx * c.x) + 'px',
            marginTop: '-' + Math.round(by * c.y) + 'px'
          });
		  
		  var cx = 30 / c.w;
          var cy = 30 / c.h;

          $('#preview-small').css({
            width: Math.round(cx * boundx) + 'px',
            height: Math.round(cy * boundy) + 'px',
            marginLeft: '-' + Math.round(cx * c.x) + 'px',
            marginTop: '-' + Math.round(cy * c.y) + 'px'
          });
		  
        }
      };*/
		
});
</script>
<div id="main">
	<div class="box clearfix">
		<div class="my-profile">
			<div class="box-header"><h1><?php echo $u['name'] ?></h1></div>
			<div class="my-header-pic"><img src="<?php echo iif(!empty($u['avatar']), _CONST_PIC_URL . 'avatar/' . $u['id'] . '/3_' . $u['avatar'], _CONST_IMG_URL . 'avatar/3.jpg')?>" width="180" /></div>
			<?php if ($uid==$_SESSION['userid']) { ?><div class="my-edit"><a href="<?php echo _CONST_WEB_URL . 'settings.php'?>" class="edit-my-profile">编辑资料</a><a href="#" id="user-upload-avatar" class="edit-my-header">上传头像</a></div><?php }?>
		</div>
		
		<div class="my-post">
			<div class="box-header"><h1><?php if($uid == $_SESSION['userid']){?>我发起的投票<?php }else{?>他发起的投票<?php }?></h1><?php if ( is_loggedin() ) { ?><input type="button" name="create-voting" class="create-voting" value="" onclick="window.location.href='<?php echo _CONST_WEB_URL . 'flow-1.php'?>'" /><?php }?></div>
			<div id="container" class="photo-wrap">
			<?php 
			$rs = get_user_vote_by_uid($uid, $page);
			if(sizeof($rs)>0){
			foreach($rs as $h){
			?>
			<div class="pin">
			<a href="<?php echo _CONST_WEB_URL . 'vote.php?id=' . $h['id']?>"><img src="<?php echo _CONST_PIC_URL . 'vote/' . $h['user_id'] . '/1_' .$h['images']?>" /></a>
			<div class="votings-info">
			<div class="votings-data">
			<span class="votings-down"><?php echo $h['sub']?></span>
			<span class="votings-up"><?php echo $h['add']?></span>
			<?php if ($uid==$_SESSION['userid']) { ?><span class="votings-del"><a href="javascript:;" onclick="vote_del(<?php echo $h['id'];?>);">删除</a></span><?php } ?>
			</div>
			</div>
			</div>
			<?php 
			}
			}
			?>
			</div>
			<div id="page-nav"><a href="ajax/homepage.php?uid=<?php echo $uid ?>u&page=<?php echo ($page-1)*5+2?>"></a></div>
			<div class="jogger" id="page-nav-real">
			<?php 
			pagination($page, 50, $count, "?uid={$uid}&page=%s%");
			?>
			</div>
		</div>
	</div>
<!--
<div class="dialog ui-draggable">
	<div class="dialog-wrap">
		<div class="dialog-title ms-yh">编辑头像</div>
		<span class="icon icon-close"></span>
		<div class="dialog-body">
			<div class="module-body-container block" id="avatar-uploader-container" style="display: block;">
				<div id="avatar-uploader-wrap" style="padding: 113.5px 0px;">
					<img src="img/flowers.jpg" id="target" alt="Flowers" />
				</div>
				<div id="avatar-uploader-preview">
					<div class="preview-big">
						<img alt="Preview" id="preview-big" src="img/flowers.jpg" style="width: 416px; height: 180px; margin-left: -118px; margin-top: 0px;">
					</div>
					<div class="preview-middle">
						<img alt="Preview" id="preview-middle" src="img/flowers.jpg" style="width: 116px; height: 50px; margin-left: -33px; margin-top: 0px;">
					</div>
					<div class="preview-small">
						<img alt="Preview" id="preview-small" src="img/flowers.jpg" style="width: 81px; height: 30px; margin-left: -23px; margin-top: 0px;">
					</div>
					<div class="preview-tip">头像将生成以上三种尺寸，请注意是否清晰。</div>
					<div class="preview-btn">
						<input type="button" id="avatar-uploader-cancel" value="取消" style="margin-right:20px;" class="fm-button-small-cancel">
						<input type="button" id="avatar-uploader-ok" value="保存" class="fm-button-small">
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
avatar-->	
</div>
<?php
//footer
include_once (_CONST_LOCAL_LAYOUT_PATH."_footer.php");

if (BENCHMARK) $timer->setMarker('page');

if (BENCHMARK) {
	echo "<!-- init: ".$timer->timeElapsed('Start', 'init')." -->\n";
	echo "<!-- auth: ".$timer->timeElapsed('init', 'auth')." -->\n";
	echo "<!-- proc: ".$timer->timeElapsed('auth', 'proc')." -->\n";
	echo "<!-- page: ".$timer->timeElapsed('proc', 'page')." -->\n";
	echo "<!-- total: ".$timer->timeElapsed('Start', 'Stop')." -->\n";
}

?>
